import { defineStore } from 'pinia'
import { LocalAuthorStoreArrType, AuthorDetail, ArticleStoreType, ShareListType, LocalArticleStoreArrType, LocalStoreArrType, HomeDeatilType, CommentDetailDataType, UserStoreArrType, UserStoreDataType } from "@/typing";
import { ref } from "vue";

// 主页的全局数据
export const homeStoriesStore = defineStore('stories', () => {
    // 当前日期
    const toggleDate = ref<string>('0');
    // 显示主页或月份页
    const isShow = ref<boolean>(false);
    // 小三角的过渡
    const isTransform = ref<boolean>(false);
    // 当前文章id
    const articleId = ref<string>('');
    // 当前文章类型
    const category = ref<string>('');
    // 当前文章标题
    const articleTitle = ref<string>('')
    // 当前文章评论数
    const commentCount = ref<number>(0)
    // 当前文章评论
    const commentList = ref<Array<CommentDetailDataType>>([])
    // discover页面的显示隐藏
    const isDiscoverShow = ref<boolean>(false);
    // 分享
    const shareList = ref<ShareListType>();

    function updateToggleData(newStories: string) {
        toggleDate.value = newStories;
    }

    function updateIsShow(newStories: boolean) {
        isShow.value = newStories;
    }

    function updateIsTransform(newStories: boolean) {
        isTransform.value = newStories;
    }

    function updateIsDiscoverShow(newStories: boolean) {
        isDiscoverShow.value = newStories;
    }

    function updateArticle(id: string, categoryStr: string, title: string) {
        articleId.value = id;
        category.value = categoryStr;
        articleTitle.value = title;
    }

    function updateComment(count: number, data: Array<CommentDetailDataType>) {
        commentCount.value = count;
        commentList.value = data;
    }

    function updateShareList(data: ShareListType) {
        shareList.value = data;
    }

    return {
        toggleDate,
        updateToggleData,

        isShow,
        isTransform,
        updateIsShow,
        updateIsTransform,

        articleTitle,
        articleId,
        category,
        updateArticle,

        commentList,
        commentCount,
        updateComment,

        isDiscoverShow,
        updateIsDiscoverShow,

        shareList,
        updateShareList,
    }
})

// 收藏/喜欢本地存储数据
export const localStoriesStore = defineStore('local', () => {
    const collectHpStoreName: string = "collectHp";
    const collectArticleStoreName: string = "collectArticle";
    const collectAuthorStoreName: string = "collectAuthor";

    // 图文的收藏
    function refreshHpCollectStore(newStories: HomeDeatilType) {
        const str = localStorage.getItem(collectHpStoreName);

        let arr: LocalStoreArrType = [];
        if (str !== null) {
            arr = JSON.parse(str);

            let tag = false;
            arr.forEach(item => {
                if (item.id == newStories.id) {
                    tag = true;
                }
            })

            if (!tag) {
                arr = [...arr, newStories]
            }
            else {
                arr = arr.filter(item => {
                    return item.id != newStories.id
                })
            }
        }
        else {
            arr = [...arr, newStories]
        }
        const collectStr = JSON.stringify(arr);
        localStorage.setItem(collectHpStoreName, collectStr);
    }
    // 图文的搜索
    function selectHpCollectId(id: string) {
        const str = localStorage.getItem(collectHpStoreName);

        let arr: LocalStoreArrType = [];
        if (str !== null) {
            arr = JSON.parse(str);
            let tag = false;

            arr.forEach(item => {
                if (item.id == id) {
                    tag = true;
                }
            })
            return tag;
        }
    }
    // 获取图文收藏列表
    function getHpCollectList() {
        const str = localStorage.getItem(collectHpStoreName);
        if (str !== null) {
            return JSON.parse(str);
        }
    }

    // 文章的收藏
    function refreshArticleCollectStore(newStories: ArticleStoreType) {
        const str = localStorage.getItem(collectArticleStoreName);

        let arr: LocalArticleStoreArrType = [];
        if (str !== null) {
            arr = JSON.parse(str);

            let tag = false;
            arr.forEach(item => {
                if (item.id == newStories.id) {
                    tag = true;
                }
            })

            if (!tag) {
                arr = [...arr, newStories]
            }
            else {
                arr = arr.filter(item => {
                    return item.id != newStories.id
                })
            }
        }
        else {
            arr = [...arr, newStories]
        }
        const collectStr = JSON.stringify(arr);
        localStorage.setItem(collectArticleStoreName, collectStr);
    }
    // 文章的搜索
    function selectArticleCollectId(id: string) {
        const str = localStorage.getItem(collectArticleStoreName);

        let arr: LocalArticleStoreArrType = [];
        if (str !== null) {
            arr = JSON.parse(str);
            let tag = false;

            arr.forEach(item => {
                if (item.id == id) {
                    tag = true;
                }
            })
            return tag;
        }
    }
    // 获取文章收藏列表
    function getArticleCollectList(category: number): LocalArticleStoreArrType {
        const str = localStorage.getItem(collectArticleStoreName);
        if (str !== null) {
            const arr: LocalArticleStoreArrType = JSON.parse(str);
            const resArr: LocalArticleStoreArrType = [];

            arr.forEach(item => {
                if (item.category === String(category)) {
                    resArr.push(item);
                }
            })
            return resArr
        }
        else {
            return []
        }
    }

    // 作者的关注
    function refreshAuthorCollectStore(newStories: AuthorDetail) {
        const str = localStorage.getItem(collectAuthorStoreName);

        let arr: LocalAuthorStoreArrType = [];
        if (str !== null) {
            arr = JSON.parse(str);

            let tag = false;
            arr.forEach(item => {
                if (item.user_id == newStories.user_id) {
                    tag = true;
                }
            })

            if (!tag) {
                arr = [...arr, newStories]
            }
            else {
                arr = arr.filter(item => {
                    return item.user_id != newStories.user_id
                })
            }
        }
        else {
            arr = [...arr, newStories]
        }
        const collectStr = JSON.stringify(arr);
        localStorage.setItem(collectAuthorStoreName, collectStr);
    }
    // 作者的搜索
    function selectAuthorCollectId(id: string) {
        const str = localStorage.getItem(collectAuthorStoreName);

        let arr: LocalAuthorStoreArrType = [];
        if (str !== null) {
            arr = JSON.parse(str);
            let tag = false;

            arr.forEach(item => {
                if (item.user_id == id) {
                    tag = true;
                }
            })
            return tag;
        }
    }
    // 作者关注列表
    function getAuthorCollectList() {
        const str = localStorage.getItem(collectAuthorStoreName);
        if (str !== null) {
            return JSON.parse(str);
        }
    }

    return {
        refreshHpCollectStore,
        selectHpCollectId,
        getHpCollectList,

        refreshArticleCollectStore,
        selectArticleCollectId,
        getArticleCollectList,

        refreshAuthorCollectStore,
        selectAuthorCollectId,
        getAuthorCollectList
    }
})

// 音频的全局数据
export const audioStoriesStore = defineStore('audio', () => {
    // 文章的播放
    const isPlay = ref<boolean>(false);
    // 音频的路径
    const audioSrc = ref<string>('');
    // auduioIcon的显示
    const showDisc = ref<boolean>(false);
    // 文章标题
    const audioTitle = ref<string>('');
    // 朗读者
    const audioAnchor = ref<string>('');
    // 时长
    const audioDuration = ref<number>(0);
    // 进度条
    const currentTime = ref<number>(0);
    // 是否拖拽进度条
    const dragProgress = ref<boolean>(false);

    // 切换暂停或播放
    function updateIsPlayOrPauseStore(newStories: boolean) {
        isPlay.value = newStories;
    }

    function updateIsAudioSrcStore(src: string, title: string, anchor: string) {
        audioSrc.value = src;
        audioTitle.value = title;
        audioAnchor.value = anchor;
    }

    function updateShowDiscStore(newStories: boolean) {
        showDisc.value = newStories;
    }

    function updateCurrentTimeStore(newStories: number) {
        currentTime.value = newStories;
    }

    function updateDurationTimeStore(time: number) {
        audioDuration.value = time;
    }

    function updateDragProgressStore(newStories: boolean) {
        dragProgress.value = newStories;
    }

    return {
        isPlay,
        updateIsPlayOrPauseStore,

        audioSrc,
        audioTitle,
        audioAnchor,
        updateIsAudioSrcStore,

        audioDuration,
        updateDurationTimeStore,

        showDisc,
        updateShowDiscStore,

        currentTime,
        updateCurrentTimeStore,

        dragProgress,
        updateDragProgressStore
    }
})

// 作者的全局数据
export const authorStoriesStore = defineStore('author', () => {
    // 作者详情显示
    // const isShowAuthor = ref<boolean>(false);
    const authorData = ref<AuthorDetail>();

    function updateAuthorData(newVal: AuthorDetail) {
        authorData.value = newVal;
    }

    return {
        authorData,
        updateAuthorData,
    }
})

// 用户的全局数据
export const userLocalStoriesStore = defineStore('user', () => {
    // 用户是否登录
    const isLogin = ref<boolean>(false);
    // 用户列表
    const userList = ref<UserStoreArrType>();
    // 用户的本地存储
    const userStoreName: string = "userStore";
    // 用户的本地存储
    const userStatusName: string = "userStatus";
    // 当前用户的本地存储
    const userNowName: string = "userNow";
    // 用户页面显示
    const isShowUser = ref<boolean>(false);


    function updateIsLogin(newStories: boolean) {
        isLogin.value = newStories;

        if (newStories) {
            updateMyUserStatus(newStories)
        } else {
            updateMyUserStatus(newStories)
        }
    }

    // 添加用户属性
    function refreshUserStore(newStories: UserStoreDataType) {
        const str = localStorage.getItem(userStoreName);

        let arr: UserStoreArrType = [];
        if (str !== null) {
            arr = JSON.parse(str);

            if (arr.indexOf(newStories) == -1) {
                arr = [...arr, newStories]
            }
        }
        else {
            arr = [...arr, newStories]
        }
        const userStr = JSON.stringify(arr);
        localStorage.setItem(userStoreName, userStr);

        refreshUserUI();
    }

    // 重新渲染用户列表
    function refreshUserUI() {
        const arr = localStorage.getItem(userStoreName)

        if (arr) {
            const userArr = JSON.parse(arr);
            userList.value = userArr;
        }
    }

    // 查询指定用户
    function searchUserList(id: string) {
        const arr = localStorage.getItem(userStoreName)

        if (arr) {
            const userArr: UserStoreArrType = JSON.parse(arr);
            let tag = false;
            if (userArr) {
                userArr.forEach(item => {
                    if (item.phone == id) {
                        tag = true
                    }
                })
                return tag
            }
        }
    }

    // 查询指定用户密码
    function searchUserPassword(id: string) {
        const arr = localStorage.getItem(userStoreName)

        if (arr) {
            const userArr: UserStoreArrType = JSON.parse(arr);
            let passwordStr = '';
            if (userArr) {
                userArr.forEach(item => {
                    if (item.phone == id) {
                        passwordStr = item.password
                    }
                })
                return passwordStr
            }
        }
    }

    // 修改当前用户
    function updateMyUserNowData(newStories: UserStoreDataType | boolean) {

        let obj: UserStoreDataType | boolean;
        if (!newStories) {
            obj = newStories
            isLogin.value = false;
        }
        else {
            obj = newStories
            isLogin.value = true;
        }
        const userStr = JSON.stringify(obj);
        localStorage.setItem(userNowName, userStr);
    }

    // 查询当前用户
    function searchUserNow() {
        const arr = localStorage.getItem(userNowName);
        if (arr) {
            const obj: UserStoreDataType = JSON.parse(arr);
            return obj
        }
    }

    // 修改当前用户状态
    function updateMyUserStatus(newStories: boolean) {
        let arr: boolean = false;
        if (!newStories) {
            arr = newStories
        }
        else {
            arr = newStories
        }
        const userStr = JSON.stringify(arr);
        localStorage.setItem(userStatusName, userStr);
    }

    function getMyUserStatus() {
        const bool = localStorage.getItem(userStatusName);
        if (bool) {
            return JSON.parse(bool);
        }
    }


    function updateIsShowUser(newStories: boolean) {
        isShowUser.value = newStories;
    }

    return {
        isLogin,
        updateIsLogin,

        userList,
        refreshUserStore,
        refreshUserUI,
        searchUserList,
        searchUserPassword,

        searchUserNow,
        updateMyUserNowData,

        isShowUser,
        updateIsShowUser,

        getMyUserStatus,
    }
})
